package com.fruit.controller.wap.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.sdk.account.usercount.UserCountService;
import com.fruit.service.user.UserManager;
import com.fruit.util.ResultInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关
 * 
 * @author Administrator
 *
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/usercount")
public class UserControllerWap extends BaseController {
	@Autowired
	private UserCountService userCountService;
	@Autowired
	private UserManager userService;
	/**
	 * 获取用户佣金统计金额
	 * @param userId
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/countUserCommissions", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> countUserCommissions(@SessionAttribute("userId")Integer userId, Page page, HttpServletRequest request, HttpSession session) {
		log.info("【用户佣金统计】---【开始】--userId={}", userId);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (userId == null) {//用户标识
				result.put("result", this.initResult(ResultInfo.ERROR, "param", "用户id不能为空"));
				return result;
			}
			//1、统计用户下级贡献佣金明细及总金额
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("start", (page.getPage() - 1) * page.getShowNum());
			pam.put("num", page.getShowNum());
			pam.put("userId", userId);
			Map<String, Object> res=userCountService.countUserCommissions(pam);
			ResultInfo commResult = (ResultInfo) res.get("result");
			if (ResultInfo.ERROR.equals(commResult.getState())) {
				result.put("result",commResult);
				return result;
			}
			
			//2、用户直属下级数量
			Map<String, Object> userRes=userService.getDownUserNum(userId);
			ResultInfo userResult = (ResultInfo) userRes.get("result");
			if (ResultInfo.ERROR.equals(userResult.getState())) {
				result.put("result",userResult);
				return result;
			}
			result.put("dataList", res.get("dataList"));//佣金明细
			result.put("money",res.get("money"));//总金额
			result.put("downUser",userRes.get("downUser"));//用户数量
			result.put("result", this.initResult(ResultInfo.SUCCESS, "ok", "操作成功！"));
		} catch (Exception e) {
			log.info("【用户佣金统计】---【异常】---{}", e);
			result.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【用户佣金统计】---【结束】--");
		return result;
	}
}
